﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Linq;
using System.Runtime.Caching;

namespace AreaHelper
{
    /// <summary>
    /// 行政区域操作类
    /// </summary>
    public class Area
    {
        static string path = Properties.Resources.Area;
        //用于缓存省市区信息
        static MemoryCache cache = new MemoryCache("Province");

        /// <summary>
        /// 省份列表
        /// </summary>
        static List<Province> Provinces
        {
            get
            {
                return GetProvince();
            }
        }

        /// <summary>
        /// 获取所有省份
        /// </summary>
        /// <returns></returns>
        public static List<Province> GetProvince()
        {
            if (cache["Province"] != null)
                return (List<Province>)cache["Province"];

            List<Province> provinces = new List<Province>();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(path);
            XmlNode root = doc.SelectSingleNode("area");
            //Console.WriteLine(root.InnerXml);
            foreach (XmlNode proN in root.SelectNodes("./province"))
            {                
                if (!(proN is XmlElement)) continue;
                XmlElement proEle = proN as XmlElement;
                //Console.WriteLine(proEle.SelectSingleNode("./name").InnerText);
                Province pro = new Province()
                {
                    Name = proEle.SelectSingleNode("./name").InnerText,
                    Code = proEle.SelectSingleNode("./code").InnerText
                };
                foreach (XmlNode cityN in proN.SelectNodes("./city"))
                {
                    if (!(cityN is XmlElement)) continue;
                    XmlElement cityEle = cityN as XmlElement;
                    City city = new City()
                    {
                        Name = cityEle.SelectSingleNode("./name").InnerText,
                        Code = cityEle.SelectSingleNode("./code").InnerText
                    };
                    foreach (XmlNode countyN in cityN.SelectNodes("./county"))
                    {
                        if (!(countyN is XmlElement)) continue;
                        XmlElement countyEle = countyN as XmlElement;
                        County county = new County()
                        {
                            Name = countyEle.SelectSingleNode("./name").InnerText,
                            Code = countyEle.SelectSingleNode("./code").InnerText
                        };
                        city.Counties.Add(county);
                    }
                    pro.Cities.Add(city);
                }
                provinces.Add(pro);
            }
            cache["Province"] = provinces;
            return provinces;
        }

        /// <summary>
        /// 根据条件查找省份，返回查找结果
        /// </summary>
        /// <param name="param">查询字符串</param>
        /// <returns></returns>
        public static List<Province> GetProvince(string param="")
        {
            param = string.IsNullOrEmpty(param) ? "" : param;
            return Provinces.Where(p => p.Name.Contains(param) || p.Code.Contains(param) 
                || p.FullPY.ToLower().StartsWith(param.ToLower()) 
                || p.AbbPY.ToLower().StartsWith(param.ToLower()) ).ToList();
        }

        /// <summary>
        /// 获取指定省份的城市，可指定查询条件
        /// </summary>
        /// <param name="province">省份</param>
        /// <param name="param">查询字符串</param>
        /// <returns></returns>
        public static List<City> GetCity(Province province, string param = "")
        {
            param = string.IsNullOrEmpty(param) ? "" : param;
            return province.Cities.Where(c => c.Name.Contains(param) || c.Code.Contains(param)
                || c.FullPY.ToLower().StartsWith(param.ToLower())
                || c.AbbPY.ToLower().StartsWith(param.ToLower())).ToList();
        }

        /// <summary>
        /// 获取指定城市的区/县，可指定查询条件
        /// </summary>
        /// <param name="city">指定城市</param>
        /// <param name="param">查询字符串</param>
        /// <returns></returns>
        public static List<County> GetCounty(City city,string param = "")
        {
            param = string.IsNullOrEmpty(param) ? "" : param;
            return city.Counties.Where(c => c.Name.Contains(param) || c.Code.Contains(param)
                || c.FullPY.ToLower().StartsWith(param.ToLower())
                || c.AbbPY.ToLower().StartsWith(param.ToLower())).ToList();
        }

    }




}
